<?php
class Search extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->model('com_model');
	}



	public function index($page = 'home')
	{
		if ( ! file_exists('application/views/pages/'.$page.'.php'))
		{
			// 页面不存在
			show_404();
		}; 
		$this->load->library('pagination');
		$keywords        = $_GET['keywords'];
		$type            = $_GET['type'];
		$reg_capital	 = '';
		$per_page        = '';
		
		if (isset($_GET['regCapital']))
		{
			$reg_capital = $_GET['regCapital'];
		}
	if (isset($_GET['per_page']))
		{
			$per_page = $_GET['per_page'];
		}
		$table_field = '';
		switch ($type) {
			case '1':
				$table_field = 'b.company_name';
				$data['search_cgy'] = '公司';
				break;
			
			case '2':
				$table_field = 'b.business_scope';
				$data['search_cgy'] = '产品';
				break;	
			
			case '3':
				$table_field = 'c.address';
				$data['search_cgy'] = '地址';
				break;
			
			default:
				return ;
		}
		$config['per_page'] = 4;
		$query = $this->com_model->get_news($table_field, $keywords, $reg_capital, $config['per_page'], $per_page);
		$data['company_lists'] = $query->result_array();
		$data['i'] = 1;
		$data['reg_capital'] = $reg_capital;
		$data['search_url'] = base_url('search').'?keywords='.$keywords.'&type='.$type;
		$replace = '<em>'.$keywords.'</em>';
		foreach ($data['company_lists'] as &$company){
			if ($type == '1') {
				$company_name = $company['company_name'];
				$company['company_name'] = str_replace($keywords, $replace, $company_name);
			}if ($type == '2') {
				$business_scope = $company['business_scope'];
				$company['business_scope'] = str_replace($keywords, $replace, $business_scope);
			}if ($type == '3') {
				$address = $company['address'];
				$company['address'] = str_replace($keywords, $replace, $address);
			}
		}
		$config['base_url'] = $data['search_url'];
		$config['enable_query_strings'] = TRUE;
		$config['page_query_string'] = TRUE;
		$config['full_tag_open'] = '<div class="pagination"><ul>';
		$config['full_tag_close'] = '</ul></div>';
		$config['total_rows'] = $this->com_model->get_news($table_field, $keywords, $reg_capital)->num_rows(); 
		$this->pagination->initialize($config); 
		$this->load->view('pages/search', $data);
	}
}
